//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    addressList:[],
    fromID: 0
  },

  selectTap: function (e) {
    let index = e.currentTarget.dataset.index
    this.changeAddress(this.data.addressList[index])
    
  },

  addAddess : function () {
    wx.navigateTo({
      url: "/pages/add-address/index?type=" + 0
    })
  },
  
  editAddess: function (e) {
    wx.navigateTo({
      url: "/pages/add-address/index?type=" + 1 +  "&id=" + e.currentTarget.dataset.id
    })
  },
  
  onLoad: function (e) {
    console.log('onLoad')
    this.setData({
      fromID: e.fromID
    })
  },
  onShow : function () {
    this.getAddressList()
  },

  /**
   * 获取地址列表
   */
  getAddressList: function () {
    let token = wx.getStorageSync('token')
    let that = this
    console.log(token)
    wx.request({
      url: app.apiUrl('/user/contactlist'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {},
      success: function (res) {
        console.log(res.data)
        if (res.data.data) {
          for (var i = 0; i < res.data.data.address_list.length; i++) {
            res.data.data.address_list[i].isDefault = res.data.data.address_list[i].id == res.data.data.default_contact_id ? true : false
            res.data.data.address_list[i].address = res.data.data.address_list[i].province + res.data.data.address_list[i].city + res.data.data.address_list[i].district + res.data.data.address_list[i].address

          }
          that.setData({
            addressList: res.data.data.address_list
          })

          //console.log(that.data.addressList)

        }
        
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  /**
   * 设置默认收货地址
   */
  setDefault: function (e) {
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    let listTemp = this.data.addressList
    for (var i = 0; i < listTemp.length; i++) {
      if (i == index) {
        listTemp[i].isDefault = true
      } else {
        listTemp[i].isDefault = false
      }
    }
    this.setData({
      addressList: listTemp
    })

    this.setDefaultAddress(id)

  },

  

  /**
   * 当前收货地址改变，回传数据给上一页
   */
  changeAddress: function (address) {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      curAddressData: address
    })

    wx.navigateBack({
      delta: 1
    })
    prevPage.getConfirmAgain()
    console.log(address)
  },

/**
 * 设置默认收货地址
 */
  setDefaultAddress: function (id) {
    let token = wx.getStorageSync('token')
    let that = this
    wx.request({
      url: app.apiUrl('/user/setdefaultcontact'),
      method: 'POST',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        contact_id: id
      },
      success: function (res) {
        console.log('设置默认收货地址')
        console.log(res.data)
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  deleteAddress: function (e) {
    let id = e.currentTarget.dataset.id
    this.deleteAddressOnServer(id)
  },

/**
 * 删除地址
 */
  deleteAddressOnServer: function (id) {
    let token = wx.getStorageSync('token')
    let that = this
    wx.request({
      url: app.apiUrl('/user/delcontact'),
      method: 'POST',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {
        contact_id: id
      },
      success: function (res) {
        console.log('删除收货地址')
        console.log(res.data)
        console.log(res.data.code)
        if (res.data.code > app.globalData.successCode) {
          wx.showToast({
            title: res.data.error_message ? res.data.error_message : app.globalData.unknownError,
            icon: 'none',
            duration: 2000
          })
        } else if (res.data.code == app.globalData.successCode) {
          app.login(that, 0)
        } else {
          //删除成功的话
          wx.showToast({
            title: '删除成功',
            icon: 'none',
            duration: 2000
          })
          that.onShow()
        }
        
      
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  }
})
